package cn.itcast.web.controller.cargo;


import cn.itcast.common.utils.BeanMapUtils;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.ShippigOrder;
import cn.itcast.domain.cargo.ShippigOrderExample;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.vo.ShippingOrderVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.URLEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
//cargo/shipping/list.do
@RequestMapping("/cargo/shipping")
public class ShippingOrderController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private PackingService packingService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size) {

        /*
        *创建example对象
        **/
        ShippigOrderExample example = null;
        PageInfo info = shippingOrderService.findAll(page, size, example);
        request.setAttribute("page",info);
        //跳转页面
        return "cargo/shippingorder/shippingorder-list";
    }

    /*
    * 添加
    * */
    @RequestMapping("/toAdd")
    public String toAdd() {
        PackingExample example = new PackingExample();
        PackingExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(0);
        List<Packing> list = packingService.findAll(example);
        request.setAttribute("packing",list);
        return "cargo/shippingorder/shippingorder-add";
    }

    /*
    * 删除
    * */
    @RequestMapping("/delete")
    public String delete(String id) {
        shippingOrderService.delete(id);
        return  "redirect:/cargo/shipping/list.do";
    }

    /*
    * 保存
    * */
    @RequestMapping("/edit")
    public String edit(ShippigOrder shippigOrder){
        shippingOrderService.save(shippigOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    /*
    * 下载Excel
    * */
    @RequestMapping("/downloadExcel")
    public void downloadExcel(String id) throws IOException {
        //1.准备数据
        ShippingOrderVo shippingOrderVo =shippingOrderService.findVoById(id);
        Map<String, Object> map = BeanMapUtils.beanToMap(shippingOrderVo);


        //List<ShippingOrderVo> list=new ArrayList<>();
        //list.add(shippingOrderVo);

        //2.设置下载信息
        response.setContentType("application/vnd.ms-excel"); //下载excel
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("委托报运单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //3.加载excel模板
        String path = session.getServletContext().getRealPath("/")+"/make/shipping.xlsx";
        //4.创建EasyExcel的excelWtire对象( 用于数据填充)
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .head(ShippingOrderVo.class) //设置表头
                .withTemplate(path) //加载模板
                .build();
        //获取sheet对象
        WriteSheet sheet = EasyExcel.writerSheet().build();
        //6.调用方法完成填充list数据
        excelWriter.fill(map,sheet);
        //7.属性资源，完成下载
        excelWriter.finish(); //下载excel文件，释放内存资源

    }

}
